#include "Utility.h"

int32_t Utility::set_nonblock(int32_t &fd)
{
    int32_t flags;

    flags = fcntl(fd,F_GETFL);

    if(flags<0)
    {
        return -1;
    }

    flags |= O_NONBLOCK;

    if(fcntl(fd,F_SETFL,flags)<0)
    {
        return -1;
    }

    return 0;
}

int32_t Utility::set_reuseaddr(int32_t &fd)
{
    int32_t re_useaddr_on = 1;

    if(setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&re_useaddr_on,sizeof(re_useaddr_on))==-1)
    {
        return -1;
    }

    return 0;
}

int32_t Utility::set_linger(int32_t &fd)
{
    struct linger ling = {1, 0};

    if(setsockopt(fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling))==-1)
	{
        return -1;
	}
    return 0;
}

